import 'package:hive/hive.dart';
import 'package:equatable/equatable.dart';

part 'training_record.g.dart';

@HiveType(typeId: 0)
class TrainingRecord extends HiveObject with EquatableMixin {
  @HiveField(0)
  final String type;

  @HiveField(1)
  final int correctCount;

  @HiveField(2)
  final int totalCount;

  @HiveField(3)
  final int durationMs; // 替换Duration为毫秒数

  @HiveField(4)
  final DateTime timestamp;

  TrainingRecord({
    required this.type,
    required this.correctCount,
    required this.totalCount,
    required this.durationMs, // 更新参数名
    required this.timestamp,
  });

  // 添加Duration getter便于使用
  Duration get duration => Duration(milliseconds: durationMs);

  double get accuracy => totalCount > 0 ? correctCount / totalCount : 0.0;

  String get formattedDuration {
    final minutes = duration.inMinutes;
    final seconds = duration.inSeconds % 60;
    if (minutes > 0) {
      return '${minutes}分${seconds}秒';
    } else {
      return '${seconds}秒';
    }
  }

  @override
  List<Object?> get props => [type, correctCount, totalCount, durationMs, timestamp];

  @override
  String toString() {
    return 'TrainingRecord{type: $type, correctCount: $correctCount, totalCount: $totalCount, duration: ${formattedDuration}, timestamp: ${timestamp.toString().substring(0, 16)}}';
  }
}